package com.huangyuan3h.StoreMarket.sinaAPI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 
 * 
 * @author huangyuan
 * 
 */
public class SinaAPI {
	private static String SinaStoreMarketURL = "http://hq.sinajs.cn/list=";// sh601006
	private static String StoreCode;

	/**
	 * 
	 * @param StoreCode
	 * @return
	 */
	public static String[] GetStoreData(String StoreCode) {
		SinaAPI.StoreCode = StoreCode;
		return formatStore(getData());
	}

	/**
	 * 
	 * @param Data
	 * @return
	 */
	private static String[] formatStore(String Data) {
		String[] x = Data.split("\"");
		if (x.length > 2)
			return x[1].split(",");
		else
			return null;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	private static String getData() {

		String result = null;
		StringBuilder responseBuilder = null;
		BufferedReader reader = null;
		OutputStreamWriter wr = null;
		URL url;

		try {
			url = new URL(SinaStoreMarketURL + StoreCode);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setConnectTimeout(1000 * 5);
			conn.setRequestProperty("contentType", "GBK");
			conn.setRequestMethod("GET");
			wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write("");
			wr.flush();
			reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "GBK"));
			responseBuilder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				responseBuilder.append(line + "\n");
			}
			wr.close();
			reader.close();
			result = responseBuilder.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}
	
	

	/**
	 * 
	 * @param ID
	 * @return
	 */
	public SH getSHByCode(String ID) {
		String code = "sh" + ID;

		SH sh = new SH();

		String[] data = GetStoreData(code);

		sh.setChineseName(data[0]);

		sh.setTodayOpening(data[1]);

		sh.setYesterdayClose(data[2]);

		sh.setCurrentPrice(data[3]);

		sh.setTodayHighest(data[4]);

		sh.setTodayLowest(data[5]);
		
		sh.setVolume(data[8] );
		
		sh.setTurnover(data[9]);
		
		sh.setDate(data[30]);
		
		sh.setTime(data[31]);

		return sh;

	}
	
	
	

	/**
	 * 
	 * @param ID
	 * @return
	 */
	public SZ getSZByCode(String ID) {
		
		String code = "sz" + ID;

		SZ sz=new SZ();

		String[] data = GetStoreData(code);

		sz.setChineseName(data[0]);

		sz.setTodayOpening(data[1]);

		sz.setYesterdayClose(data[2]);

		sz.setCurrentPrice(data[3]);

		sz.setTodayHighest(data[4]);

		sz.setTodayLowest(data[5]);

		sz.setVolume(data[8] );
		
		sz.setTurnover(data[9]);
		
		sz.setDate(data[30]);
		
		sz.setTime(data[31]);
		
		return sz;
	}
	
	
	/**
	 * 
	 * @param ID
	 * @return
	 */
	public HK getHKByCode(String ID) {
		
		String code = "hk" + ID;

		HK hk=new HK();

		String[] data = GetStoreData(code);
		
		hk.setEnglishName(data[0]);

		hk.setChineseName(data[1]);

		hk.setTodayOpening(data[2]);

		hk.setYesterdayClose(data[3]);
		
		hk.setTodayHighest(data[4]);
		
		hk.setTodayLowest(data[5]);

		hk.setCurrentPrice(data[6]);
		
		hk.setVolume(data[12]);
		
		hk.setTurnover(data[11]);
		
		hk.setDate(data[17]);
		
		hk.setTime(data[18]);

		return hk;
	}
	
	
	

	/***
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String[] x = GetStoreData("hk00700");
		int i = 0;
		for (String string : x) {
			System.out.println(i + ":" + string);
			i++;
		}

	}

}
